package com.hs.panghu.dao.imp;

import com.hs.panghu.dao.GoodsDao;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
//
    @Override
    public int addGoods(Goods goods) {
        return 0;
    }
// 上下架
    @Override
    public int editGoods(Goods goods) {
        String sql = "update goods set goodsStatus = ? where goodsId = ?";
        Object[] objects = {
                goods.getGoodsStatus(),
                goods.getGoodsId()
        };
        int row = JDBCDruidUtil.update(sql, objects);
        return row;
    }
//删除
    @Override
    public int delGoods(Goods Goods) {
        return 0;
    }
//查询所有商品
    @Override
    public List<Goods> queryGoods(Map<String, String> map) {
        List<Goods> list = new ArrayList<>();
//        动态sql和动态数组
        StringBuffer sql = new StringBuffer("select * from goods where 1=1 ");
        List param = new ArrayList();
//        判断goodsName是否有值
        String goodsName = map.get("goodsName");
        if(goodsName != null && !"".equals(goodsName)){
            sql.append(" and goodsName like ?");
            param.add("%"+goodsName+"%");
        }
//        判断shopName是否有值
        String shopName = map.get("shopName");
        if(shopName != null && !"".equals(shopName)){
            sql.append(" and goodsShopName like ?");
            param.add("%"+shopName+"%");
        }
//        判断日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime != null && !"".equals(startTime)){
            sql.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
//        判断商品状态
        String goodsStatus = map.get("goodsStatus");
        if(goodsStatus != null && !"".equals(goodsStatus)){
            sql.append(" and goodsStatus = ? ");
            param.add(goodsStatus);
        }
//        分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sql.append(" order by goodsId desc limit ?,?");
        param.add((pageNum-1)*pageSize);
        param.add(pageSize);
//        封装sql和object 调用工具类
        String sql2 = sql.toString();
        Object object[] = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql2, object);
        try{
            while (resultSet.next()){
                Goods good = new Goods(
                        resultSet.getDate(1),
                        resultSet.getString(2),
                        resultSet.getInt(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getInt(6),
                        resultSet.getDouble(7),
                        resultSet.getString(8),
                        resultSet.getInt(9),
                        resultSet.getString(10),
                        resultSet.getInt(11),
                        resultSet.getInt(12)
                );
                list.add(good);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }
//查询总条数
    @Override
    public int queryCounts(Map<String, String> map) {
        int count = 0;
        //        动态sql和动态数组
        StringBuffer sql = new StringBuffer("select count(1) from goods where 1=1 ");
        List param = new ArrayList();
//        判断goodsName是否有值
        String goodsName = map.get("goodsName");
        if(goodsName != null && !"".equals(goodsName)){
            sql.append(" and goodsName like ?");
            param.add("%"+goodsName+"%");
        }
//        判断shopName是否有值
        String shopName = map.get("shopName");
        if(shopName != null && !"".equals(shopName)){
            sql.append(" and goodsShopName like ?");
            param.add("%"+shopName+"%");
        }
//        判断日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime != null && !"".equals(startTime)){
            sql.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
//        判断商品状态
        String goodsStatus = map.get("goodsStatus");
        if(goodsStatus != null && !"".equals(goodsStatus)){
            sql.append(" and goodsStatus = ? ");
            param.add(goodsStatus);
        }


//        封装sql和object 调用工具类
        String sql2 = sql.toString();
        Object object[] = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql2, object);
        try{
            if(resultSet.next()){
                    count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
